<template>
  <!--所有的item都展示同一个图片, 同一个文字-->
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle" :class="{itemActive:active}"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
  export default {
    name: "TabBarItem",
    props: {
      path: String,
      activeColor: {
        type: String,
        default: 'black'
      },
      fontSize:{
        type: String,
        default: ''
      }
    },
    data() {
      return {
         active: true
      }
    },
    computed: {
      isActive() {

        if(this.$route.path.indexOf(this.path) !== -1){
             this.active = true
        }else{
          this.active =false
        }
        return this.active
      },
      activeStyle() {
        return this.isActive ? {color: this.activeColor,fontSize:this.fontSize} : {}
      }
    },
    methods: {
      itemClick() {     //点击切换路由
        // console.log(this.active);
        if(!this.active){  //解决重复点击报错的问题
          this.$router.replace(this.path)
        }
        // console.log(this.$route.path);
        // console.log(this.path);
      }
    }
  }
</script>

<style scoped>
  .tab-bar-item {
    flex: 1;
    text-align: center;
    height: 49px;
    font-size: 14px;
    color: #c1c1c1;
  }

  .tab-bar-item img {
    width: 24px;
    height: 24px;
    margin-top: 3px;
    vertical-align: middle;
    margin-bottom: 2px;
  }
</style>
